const app = getApp();

Page({
  data: {
    xiangmumingcheng: '',
    members: [],
    applications: [],
    isFuzeren: false
  },

  onLoad: function(options) {
    if (options.xiangmumingcheng) {
      this.setData({
        xiangmumingcheng: options.xiangmumingcheng
      });
      this.loadTeamInfo();
      this.checkUserRole();
    }
  },

  // 加载团队信息
  loadTeamInfo: function() {
    wx.showLoading({
      title: '加载中...',
    });

    wx.request({
      url: app.globalData.baseUrl + '/xiangmushenqing/team/' + this.data.xiangmumingcheng,
      method: 'GET',
      success: (res) => {
        if (res.data.code === 0) {
          this.setData({
            members: res.data.data.members || [],
            applications: res.data.data.applications || []
          });
        } else {
          wx.showToast({
            title: '加载失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      },
      complete: () => {
        wx.hideLoading();
      }
    });
  },

  // 检查用户角色
  checkUserRole: function() {
    const userInfo = wx.getStorageSync('userInfo');
    if (userInfo && userInfo.tableName === 'fuzeren') {
      this.setData({
        isFuzeren: true
      });
    }
  },

  // 处理通过申请
  handleApprove: function(e) {
    const id = e.currentTarget.dataset.id;
    this.handleApplication(id, '是', '申请已通过');
  },

  // 处理拒绝申请
  handleReject: function(e) {
    const id = e.currentTarget.dataset.id;
    this.handleApplication(id, '否', '申请已拒绝');
  },

  // 处理申请
  handleApplication: function(id, sfsh, shhf) {
    wx.showLoading({
      title: '处理中...',
    });

    wx.request({
      url: app.globalData.baseUrl + '/xiangmushenqing/shBatch',
      method: 'POST',
      data: {
        ids: [id],
        sfsh: sfsh,
        shhf: shhf
      },
      success: (res) => {
        if (res.data.code === 0) {
          wx.showToast({
            title: '处理成功',
            icon: 'success'
          });
          // 重新加载团队信息
          this.loadTeamInfo();
        } else {
          wx.showToast({
            title: '处理失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      },
      complete: () => {
        wx.hideLoading();
      }
    });
  }
}); 